package com.example.myapp;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.math.BigDecimal;

public class MainActivity extends AppCompatActivity {
    private static final String TAG ="MainActivity";
    String my_age,my_weight,my_height;
String my_sex,my_type,str,advise;
TextView show_BMI,show_BMR,show_state,show_kal;
    RadioGroup sex,type;
    RadioButton state1,state2,state3;
Double bmi,sta,bmr;
    @Override


        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        show_BMI = findViewById(R.id.show_bmi);
        show_BMR = findViewById(R.id.show_bmr);
        show_state = findViewById(R.id.show_state);
        show_kal = findViewById(R.id.show_kal);

        state1 = findViewById(R.id.state_increse);
        state2 = findViewById(R.id.state_incresemax);
        state3 = findViewById(R.id.state_decrese);

        calculate();
        ImageButton image_my = findViewById(R.id.main_my);
        ImageButton image_add = findViewById(R.id.main_add);
        ImageButton image_home = findViewById(R.id.main_home);

        image_my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent home1 = new Intent(MainActivity.this, List1Activity.class);//跳转到的页面，必备
                startActivityForResult(home1, 4);
            }
        });
        image_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent home1 = new Intent(MainActivity.this, InputStateActivity.class);//跳转到的页面，必备
                startActivityForResult(home1, 4);
            }
        });
        SharedPreferences sharedPreferences = getSharedPreferences("mystate", Activity.MODE_PRIVATE);
        my_age = sharedPreferences.getString("myage", "");
        my_weight = sharedPreferences.getString("myweight", "");
        my_height = sharedPreferences.getString("myheight", "");
        my_sex = sharedPreferences.getString("mysex", "");
        my_type = sharedPreferences.getString("mytype", "");
        //输出bmi
        bmi = Double.valueOf(my_weight) / (Double.valueOf(my_height) * 0.01 * Double.valueOf(my_height) * 0.01);
        BigDecimal b = new BigDecimal(bmi);
        //保留2位小数
        bmi = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        if (bmi <= 18.4) {
            str = "您的身体状态：偏瘦,建议多吃点，保持健康体重";
        } else if (bmi <= 23.9) {
            str = "您的身体状态：正常，现在的状态很好，继续保持哦";
        } else
            str = "您的身体状态：偏重，当前体重偏重，可能会影响健康，建议减重";

        //输出bmr
        if (my_type.equals("坐式生活方式（极少运动）")) {
            sta = 1.15;
        } else if (my_type.equals("低强度健身（每周运动1-2次）")) {
            sta = 1.3;
        } else if (my_type.equals("中强度健身（每周运动3-5次）")) {
            sta = 1.4;
        } else {
            sta = 1.6;
        }

        if (my_sex.equals("男")) {
            bmr = (67 + 13.73 * Double.valueOf(my_weight) + 5 * Double.valueOf(my_height) - 6.9 * Double.valueOf(my_age)) * sta;

            BigDecimal c = new BigDecimal(bmr);
            //保留2位小数
            bmr = c.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();

        } else if (my_sex.equals("女")) {
            bmr = (661 + 9.6 * Double.valueOf(my_weight) + 1.72 * Double.valueOf(my_height) * 0.01 - 4.7 * Double.valueOf(my_age)) * sta;
            BigDecimal c = new BigDecimal(bmr);
            //保留2位小数
            bmr = c.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        }

        show_BMI.setText(String.valueOf(bmi));
        show_BMR.setText(String.valueOf(bmr));
        show_state.setText(str);



    }
    public void main_submit(View btn) {

        if (state1.isChecked() == true) {
            double ah = 0.0;
            BigDecimal ch = new BigDecimal(bmr);
            //保留2位小数
            ah = ch.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            show_kal.setText(String.valueOf(ah * 0.8));

        } else if (state2.isChecked() == true) {
            double ai = 0.0;
            BigDecimal ci = new BigDecimal(bmr);
            //保留2位小数
            ai = ci.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            show_kal.setText(String.valueOf(ai * 0.6));
        } else {
            double ab = 0.0;
            BigDecimal cb = new BigDecimal(bmr);
            //保留2位小数
            ab = cb.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            show_kal.setText(String.valueOf(ab * 1.2));
        }
        }




    void calculate() {

        //下方菜单栏

    }}

